var express = require('express');
var router = express.Router();
const orderService = require('../service/orderService.js');
const snapService = require('../service/snapService.js');
/**
 * @swagger
 * /order/:
 *    get:
 *      summary: 查找订单
 *      tags:
 *       - order
 *      parameters:
 *      - name: type
 *        in: query
 *        description: 搜索类型
 *        required: false
 *      - name: value
 *        in: query
 *        description: 搜索值
 *        required: false
 *      - name: current
 *        in: query
 *        description: 当前页
 *        required: false
 *      - name: pageSize
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/', async function (req, res, next) {
    let {
        type,
        value,
        current,
        size
    } = req.query;
    console.log(type, value);
    let searchObj = {};
    if (type) {
        if (type == 'users') {
            searchObj[type] = value;
        } else {
            searchObj[type] = {
                $regex: value
            };
        }
    }

    let data = await orderService.findAll(searchObj, {
        current: current || 1,
        size: size || 5
    });

    res.send(data);
});
/**
 * @swagger
 * /order:
 *    post:
 *      tags:
 *      - order
 *      summary: 增加订单
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                time:          
 *                  type: string
 *                name:          
 *                  type: string
 *                price:
 *                  type: string
 *                total:          
 *                  type: string
 *                shopImg:          
 *                  type: string
 *                isStatue:
 *                  type: boolean
 *                sale:
 *                  type: string
 *                shopId:
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.post('/', async function (req, res) {
    let {
        time,
        state,
        users,
        shopData
        // name,
        // price,
        // types,
        // total,
        // shopImg,
        // isStatue,
        // sale,
        // shopId
    } = req.body;
    console.log(time, state, users,shopData);
    let orderData = await orderService.insert({
        time,
        state,
        users
    });
    let data=[];
    for (let i = 0; i < shopData.length; i++) {
        let snapData = await snapService.createSnap({
            ...shopData[i],
            order: orderData._id
        });
        data.push(snapData);
    }
    
    res.send(data);
});

/**
 * @swagger
 * /order/{id}:
 *    put:
 *      tags:
 *      - order
 *      summary: 修改订单
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 订单id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                time:          
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.put('/:id', async function (req, res) {
    let id = req.params.id;
    console.log(id, req.body);
    let data = await orderService.update(id, req.body);
    res.send(data);
});

/**
 * @swagger
 * /order/{id}:
 *    delete:
 *      summary: 删除订单
 *      tags:
 *       - order
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 订单id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.delete('/:id', async function (req, res) {
    let data = await orderService.delete(req.params.id);
    res.send(data);
})



router.get('/:snapId', async function (req, res) {
    let data = await snapService.findByOrderId(req.params.snapId);
    res.send(data);
})

router.get('/getOne/:_id', async function (req, res) {
    let {
        _id
    } = req.params;
    console.log(_id);
    let data = await orderService.findById(_id);
    res.send({
        data
    });
})

module.exports = router;